/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import info.peigen.hotpot.manage.core.domain.Relation;
import info.peigen.hotpot.manage.core.repository.entity.RelationEntity;
import info.peigen.hotpot.manage.core.repository.mapper.RelationMapper;
import info.peigen.hotpot.manage.core.util.convertor.RelationConvertor;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(RelationRepository)</b>
 * "关联关系" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-06-28
 */
@SuppressWarnings("unused")
@Component
public class RelationRepository extends AbstractBeetlSqlRepository<RelationMapper, RelationEntity> {
    public Relation active(String relationId) {
        Assert.isTrue(StrUtil.isNotEmpty(relationId), () -> new BusinessException(ResultCode.FAILURE, "激活时[relationId]不得为空!"));
        RelationEntity entity = getMapper().findByRelationId(relationId);
        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!relationId=" + relationId));

        return RelationConvertor.INSTANCE.convertToDomain(entity);
    }

    public List<Relation> active(List<String> relationIds) {
        Assert.isTrue(CollUtil.isNotEmpty(relationIds), () -> new BusinessException(ResultCode.FAILURE, "激活时[relationIds]不得为空!"));
        List<RelationEntity> entities = getMapper().listByRelationIds(relationIds);
        Assert.notNull(entities, () -> new BusinessException(ResultCode.FAILURE, "激活失败!relationId=" + relationIds));

        return RelationConvertor.INSTANCE.convertToDomainsByEntity(entities);

    }

    public Relation lock(String relationId) {
        Assert.isTrue(StrUtil.isNotEmpty(relationId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[relationId]不得为空!"));
        RelationEntity entity = getMapper().lockByRelationId(relationId);
        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!relationId为空!"));
        return RelationConvertor.INSTANCE.convertToDomain(entity);
    }

    public void removeByRelationId(String relationId) {
        Assert.isTrue(StrUtil.isNotEmpty(relationId), () -> new BusinessException(ResultCode.FAILURE, "删除时[relationId]不得为空!"));
        getMapper().deleteByRelationId(relationId);
    }

    public void logicDeleteById(Integer id){
        delete(id);
    }

    public int logicDeleteByRelationId(String relationId) {
        Assert.isTrue(StrUtil.isNotEmpty(relationId), () -> new BusinessException(ResultCode.FAILURE, "逻辑删除时[relationId]不得为空!"));
        return getMapper().createLambdaQuery().andEq(RelationEntity::getRelationId, relationId).delete();
    }

    public void store(Relation domain){
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));
        RelationEntity entity = RelationConvertor.INSTANCE.convertToEntity(domain);
        saveOrUpdate(entity);
    }

}